package com.systemCapacity.controller;

import com.systemCapacity.common.dto.AddBattleResolveDto;
import com.systemCapacity.common.dto.BattleCapacityResolveDto;
import com.systemCapacity.common.lang.Result;
import com.systemCapacity.service.BattleCapacityResolveService;
import com.systemCapacity.service.BattleTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/capacity/resolve")
public class BattleCapacityResolveController {

    @Autowired
    BattleTaskService battleTaskService;

    @Autowired
    BattleCapacityResolveService battleCapacityResolveService;

    /**
     * 查看能力分解树
     * @return 能力分解树
     */
    @GetMapping("/tree")
    public Result getResolveTree() {
        List<BattleCapacityResolveDto> tree = battleCapacityResolveService.getTree();
        return Result.succ(tree);
    }

    /**
     * 编辑能力分解
     * @param dtos 作战能力分解结果
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result editBattleCapacityResolve(@RequestBody @Validated List<BattleCapacityResolveDto> dtos) {
        if (dtos.size() != 10) {
            return Result.fail("请选择所有十大能力");
        }
        // 先删除，再添加
        battleCapacityResolveService.deleteResolve(dtos.get(0).getTaskId());
        battleCapacityResolveService.addBattleResolve(dtos);
        return Result.succ(null);
    }

    /**
     * 删除能力分解结果
     * @param taskId 作战任务id
     * @return 删除结果
     */
    @GetMapping("/delete")
    public Result deleteBattleResolve(@RequestParam(value = "taskId") String taskId) {
        if (!battleTaskService.taskExist(taskId)) {
            return Result.fail("作战任务不存在，请重新选择");
        }
        battleCapacityResolveService.deleteResolve(taskId);
        return Result.succ(null);
    }

    /**
     * 保存能力分解结果
     * @param dto 作战能力添加结构体
     * @return 保存结果
     */
    @PostMapping("/save")
    public Result addBattleCapacityResolve(@RequestBody AddBattleResolveDto dto) {
        if (dto.getTaskId() == null || !battleTaskService.taskExist(dto.getTaskId())) {
            return Result.fail("所选作战任务不能为空");
        }
        battleCapacityResolveService.deleteResolve(dto.getTaskId());
        battleCapacityResolveService.addBattleResolveByIds(dto.getIds(), dto.getTaskId());
        return Result.succ(null);
    }

    /**
     * 查询作战任务能力分解结果
     * @param taskId 作战任务id
     * @return 作战任务能力分解结果
     */
    @GetMapping("/list")
    public Result getBattleCapacityResolve(@RequestParam(value = "taskId") String taskId) {
        if (!battleTaskService.taskExist(taskId)) {
            return Result.fail("所选作战任务不能为空");
        }
        return Result.succ(battleCapacityResolveService.getResolve(taskId));
    }

    /**
     * 查询能力分解下拉菜单
     * @return 能力下拉菜单
     */
    @GetMapping("/menu")
    public Result getCapacitiesMenu(@RequestParam(value = "taskId") String taskId) {
        if (!battleTaskService.taskExist(taskId)) {
            return Result.fail("当前作战任务不存在");
        }
        return Result.succ(battleCapacityResolveService.getCapacitiesMenu(taskId));
    }
}
